 package com.shopping.manage.seller.action;
 
 import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.nutz.json.Json;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.shopping.core.annotation.SecurityMapping;
import com.shopping.core.domain.virtual.SysMap;
import com.shopping.core.mv.JModelAndView;
import com.shopping.core.query.support.IPageList;
import com.shopping.core.security.support.SecurityUserHolder;
import com.shopping.core.tools.CommUtil;
import com.shopping.core.tools.WebForm;
import com.shopping.core.tools.database.DatabaseTools;
import com.shopping.foundation.domain.Vehicle;
import com.shopping.foundation.domain.Area;
import com.shopping.foundation.domain.query.VehicleQueryObject;
import com.shopping.foundation.service.IVehicleService;
import com.shopping.foundation.service.IAreaService;
import com.shopping.foundation.service.ISysConfigService;
import com.shopping.foundation.service.IUserConfigService;
 
 @Controller
 public class VehicleBuyerAction
 {
 
   @Autowired
   private ISysConfigService configService;
 
   @Autowired
   private IUserConfigService userConfigService;
 
   @Autowired
   private IVehicleService vehicleService;
 
   @Autowired
   private IAreaService areaService;
 
   @Autowired
   private DatabaseTools databaseTools;
 
   @SecurityMapping(display = false, rsequence = 0, title="车辆列表", value="/seller/vehicle.htm*", rtype="seller", rname="用户中心", rcode="user_center", rgroup="用户中心")
   @RequestMapping({"/seller/vehicle.htm"})
   public ModelAndView vehicle(HttpServletRequest request, HttpServletResponse response, String currentPage, String orderBy, String orderType)
   {
     ModelAndView mv = new JModelAndView("user/default/usercenter/vehicle.html", this.configService.getSysConfig(), 
       this.userConfigService.getUserConfig(), 0, request, response);
     String shopping_view_type = CommUtil.null2String( request.getSession( false ).getAttribute( "shopping_view_type" ) );
	 if( (shopping_view_type != null) && (!shopping_view_type.equals( "" )) && (shopping_view_type.equals( "wap" )) ) {
		 mv = new JModelAndView("wap/vehicle.html", this.configService.getSysConfig(), 
			       this.userConfigService.getUserConfig(), 1, request, response);
	 }
     String url = this.configService.getSysConfig().getAddress();
     if ((url == null) || (url.equals(""))) {
       url = CommUtil.getURL(request);
     }
     String params = "";
     VehicleQueryObject qo = new VehicleQueryObject(currentPage, mv, orderBy, orderType);
     qo.addQuery("obj.store.id", new SysMap("store_id", SecurityUserHolder.getCurrentUser().getStore().getId()), "=");
     IPageList pList = this.vehicleService.list(qo);
     CommUtil.saveIPageList2ModelAndView(url + "/seller/vehicle.htm", "", params, pList, mv);
     return mv;
   }
   //轨迹管理
   @SecurityMapping(display = false, rsequence = 0, title="车辆轨迹地图", value="/seller/vehicleLocation.htm*", rtype="seller", rname="用户中心", rcode="user_center", rgroup="用户中心")
   @RequestMapping({"/seller/vehicleLocation.htm"})
   public ModelAndView vehicleLocation(HttpServletRequest request, HttpServletResponse response, String currentPage, String orderBy, String orderType)
   {
	   ModelAndView mv = new JModelAndView("user/default/usercenter/vehicleLocation.html", this.configService.getSysConfig(), 
			   this.userConfigService.getUserConfig(), 0, request, response);
	   String shopping_view_type = CommUtil.null2String( request.getSession( false ).getAttribute( "shopping_view_type" ) );
	   if( (shopping_view_type != null) && (!shopping_view_type.equals( "" )) && (shopping_view_type.equals( "wap" )) ) {
		   mv = new JModelAndView("wap/vehicle.html", this.configService.getSysConfig(), 
				   this.userConfigService.getUserConfig(), 1, request, response);
	   }
	    
	   return mv;
   }
    /**
     * 新增车辆页面
	 * @param request
	 * @param response
	 * @param currentPage
	 * @return
	 */
   @SecurityMapping(display = false, rsequence = 0, title="新增车辆", value="/seller/vehicle_add.htm*", rtype="seller", rname="用户中心", rcode="user_center", rgroup="用户中心")
   @RequestMapping({"/seller/vehicle_add.htm"})
   public ModelAndView vehicle_add(HttpServletRequest request, HttpServletResponse response, String currentPage) {
     ModelAndView mv = new JModelAndView("user/default/usercenter/vehicle_add.html", this.configService.getSysConfig(), 
       this.userConfigService.getUserConfig(), 0, request, response);
     String shopping_view_type = CommUtil.null2String( request.getSession().getAttribute( "shopping_view_type" ) );
	 if( (shopping_view_type != null) && (!shopping_view_type.equals( "" )) && (shopping_view_type.equals( "wap" )) ) {
		 mv = new JModelAndView("wap/vehicle_add.html", this.configService.getSysConfig(), 
			       this.userConfigService.getUserConfig(), 1, request, response);
	 }
     mv.addObject("currentPage", currentPage);
     return mv;
   }
   @SecurityMapping(display = false, rsequence = 0, title="新增车辆", value="/seller/vehicle_edit.htm*", rtype="seller", rname="用户中心", rcode="user_center", rgroup="用户中心")
   @RequestMapping({"/seller/vehicle_edit.htm"})
   public ModelAndView vehicle_edit(HttpServletRequest request, HttpServletResponse response, String id, String currentPage) {
     ModelAndView mv = new JModelAndView("user/default/usercenter/vehicle_add.html", this.configService.getSysConfig(), 
       this.userConfigService.getUserConfig(), 0, request, response);
     String shopping_view_type = CommUtil.null2String( request.getSession().getAttribute( "shopping_view_type" ) );
	 if( (shopping_view_type != null) && (!shopping_view_type.equals( "" )) && (shopping_view_type.equals( "wap" )) ) {
		 mv = new JModelAndView("wap/vehicle_add.html", this.configService.getSysConfig(), 
			       this.userConfigService.getUserConfig(), 1, request, response);
	 }
     Vehicle obj = this.vehicleService.getObjById(CommUtil.null2Long(id));
     mv.addObject("obj", obj);
     mv.addObject("currentPage", currentPage);
     return mv;
   }
 
   @SecurityMapping(display = false, rsequence = 0, title="车辆保存", value="/seller/vehicle_save.htm*", rtype="seller", rname="用户中心", rcode="user_center", rgroup="用户中心")
   @RequestMapping({"/seller/vehicle_save.htm"})
   public String vehicle_save(HttpServletRequest request, HttpServletResponse response, String id, String area_id, String currentPage)
   {
     WebForm wf = new WebForm();
     Vehicle vehicle = null;
     if (id.equals("")) {
       vehicle = (Vehicle)wf.toPo(request, Vehicle.class);
       vehicle.setAddTime(new Date());
     } else {
       Vehicle obj = this.vehicleService.getObjById(Long.valueOf(Long.parseLong(id)));
       vehicle = (Vehicle)wf.toPo(request, obj);
     }
     vehicle.setUser(SecurityUserHolder.getCurrentUser());
     vehicle.setStore(SecurityUserHolder.getCurrentUser().getStore());
     if (id.equals(""))
       this.vehicleService.save(vehicle);
     else
       this.vehicleService.update(vehicle);
     return "redirect:vehicle.htm?currentPage=" + currentPage;
   }
   @SecurityMapping(display = false, rsequence = 0, title="车辆删除", value="/seller/vehicle_del.htm*", rtype="seller", rname="用户中心", rcode="user_center", rgroup="用户中心")
   @RequestMapping({"/seller/vehicle_del.htm"})
   public String vehicle_del(HttpServletRequest request, HttpServletResponse response, String mulitId, String currentPage) {
     String[] ids = mulitId.split(",");
     for (String id : ids) {
       if (!id.equals("")) {
         this.vehicleService.delete(Long.valueOf(Long.parseLong(id)));
       }
     }
     return "redirect:vehicle.htm?currentPage=" + currentPage;
   }
   
//	@SecurityMapping(display = false, rsequence = 0, title = "加载车辆", value = "/seller/load_vehicle_auto.htm*", rtype = "seller", rname = "用户中心", rcode = "goods_seller", rgroup = "商品管理")
	@RequestMapping({ "/seller/load_vehicle_auto.htm" })
	public void load_vehicle_auto(HttpServletRequest request,
			HttpServletResponse response, String name) {
		Map<String,Object> map = new HashMap<String,Object>();
		Map params = new HashMap();
		params.put("user",SecurityUserHolder.getCurrentUser().getId());
		if ((name != null) && (!name.equals(""))) {
			params.put("car_num", "%"+name+"%");
			params.put("name",  "%"+name+"%");
		}
//		else{
//			params.put("className", "12121212");
//			params.put("pinyi", "12121212");
//		}
		
		List<Map> list =new ArrayList();
//		List pList = this.goodsClassService.query("select obj from Vehicle obj where obj.className like:className or obj.pinyi like:pinyi",
				List pList = this.vehicleService.query("select obj from Vehicle obj where obj.user.id=:user ",
				params, -1, -1);
		 for(int i=0; i<pList.size();i++){
			 Vehicle vehicle = (Vehicle) pList.get(i);
			 Map mapVehicle =new HashMap();
			 mapVehicle.put("name", vehicle.getName());
			 mapVehicle.put("car_num", vehicle.getCar_num());
			 mapVehicle.put("mobile", vehicle.getMobile());
			 list.add(mapVehicle);
		 }
		response.setContentType("text/plain");
		response.setHeader("Cache-Control", "no-cache");
		response.setCharacterEncoding("UTF-8");
		try {
			PrintWriter writer = response.getWriter();
			writer.print(Json.toJson(list));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
 }


 
 
 